package cn.sfrog.system.service.impl;

import cn.sfrog.common.utils.SecurityUtils;
import cn.sfrog.system.domain.SysSensitiveWord;
import cn.sfrog.system.domain.vo.BizStatusChangeVO;
import cn.sfrog.system.domain.vo.SysSensitiveWordFormVO;
import cn.sfrog.system.domain.vo.SysSensitiveWordQueryVO;
import cn.sfrog.system.domain.vo.SysSensitiveWordVO;
import cn.sfrog.system.mapper.SysSensitiveWordMapper;
import cn.sfrog.system.service.ISysSensitiveWordService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 敏感词Service业务层处理
 */
@Service
public class SysSensitiveWordServiceImpl extends ServiceImpl<SysSensitiveWordMapper, SysSensitiveWord> implements ISysSensitiveWordService {
    @Resource
    private SysSensitiveWordMapper sysSensitiveWordMapper;

    /**
     * 查询敏感词
     *
     * @param id 敏感词主键
     * @return 敏感词
     */
    @Override
    public SysSensitiveWordVO selectSysSensitiveWordById(Long id) {
        return sysSensitiveWordMapper.selectSysSensitiveWordById(id);
    }

    /**
     * 查询敏感词列表
     *
     * @param query 敏感词
     * @return 敏感词
     */
    @Override
    public List<SysSensitiveWordVO> selectSysSensitiveWordList(SysSensitiveWordQueryVO query) {
        return sysSensitiveWordMapper.selectSysSensitiveWordList(query);
    }

    /**
     * 新增敏感词
     *
     * @param form 敏感词
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean insertSysSensitiveWord(SysSensitiveWordFormVO form) {
        SysSensitiveWord sysSensitiveWord = new SysSensitiveWord();
        BeanUtils.copyProperties(form, sysSensitiveWord);
        sysSensitiveWord.setCreateTime(new Date());
        sysSensitiveWord.setCreateBy(SecurityUtils.getUsername());
        sysSensitiveWord.setUpdateTime(new Date());
        sysSensitiveWord.setUpdateBy(SecurityUtils.getUsername());
        return sysSensitiveWord.insert();
    }

    /**
     * 修改敏感词
     *
     * @param form 敏感词
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean updateSysSensitiveWord(SysSensitiveWordFormVO form) {
        SysSensitiveWord sysSensitiveWord = new SysSensitiveWord();
        BeanUtils.copyProperties(form, sysSensitiveWord);
        sysSensitiveWord.setUpdateTime(new Date());
        sysSensitiveWord.setUpdateBy(SecurityUtils.getUsername());
        return sysSensitiveWord.updateById();
    }

    /**
     * 批量删除敏感词
     *
     * @param ids 需要删除的敏感词主键
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean deleteSysSensitiveWordByIds(Long[] ids) {
            return SysSensitiveWord.DAO.delete(Wrappers.lambdaQuery(SysSensitiveWord.class).in(SysSensitiveWord::getId, (Object[]) ids));
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean changeStatus(BizStatusChangeVO vo) {
        return SysSensitiveWord.DAO.update(Wrappers.lambdaUpdate(SysSensitiveWord.class).in(SysSensitiveWord::getId, (Object[]) vo.getIds()).set(SysSensitiveWord::getStatus, vo.getStatus()));
    }
}
